﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    public int creatorId;
    public float speed;
    public Vector3 dir;
    // Start is called before the first frame update
    public Team cachedTeam {
        get {
            return GetComponent<Team>();
        }
    }
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 delta = dir * (speed * Time.deltaTime);
        Vector3 pos = transform.localPosition + delta;
        transform.localPosition += pos;
        Plane hitted = _GetHittedPlane();
        if(hitted)
        {
            hitted.Boom();
            Boom();
            return;
        }
    }

    public void Boom()
    {
        GameObject.DestroyImmediate(gameObject);
    }

    private Plane _GetHittedPlane() 
    {
        for(int i = 0; i < Team._allTeams.Count; i++)
        {
            Team t = Team._allTeams[i];
            if(Team.IsEnemy(cachedTeam.team, t.team))
            {
                //destory it
                Plane p = t.GetComponent<Plane>();
                if(p != null)
                {
                    return p;
                }
            }
        }
        return null;
    }
}
